/*
 *  Copyright 2005 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.atjava.nebula.core.util.bean;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;

/**
 * @author kyo
 */
public class JavaBeansUtil {

	/**
	 *  
	 */
	private JavaBeansUtil() {
		super();
	}



	/**按照Bean的规范对类名进行格式化，如把表名转换为Bean的类名
	 * @param inputString 需要格式化的字符串
	 * @param firstCharacterUppercase 是否首字符大写
	 * @return 返回格式化的结果字符串
	 */
	public static String getCamelCaseString(String inputString,
			boolean firstCharacterUppercase) {
		StringBuffer sb = new StringBuffer();

		boolean nextUpperCase = false;
		for (int i = 0; i < inputString.length(); i++) {
			char c = inputString.charAt(i);

			switch (c) {
			case '_':
			case '-':
            case '@':
            case '$':
            case '#':
            case ' ':
				if (sb.length() > 0) {
					nextUpperCase = true;
				}
				break;

			default:
				if (nextUpperCase) {
					sb.append(Character.toUpperCase(c));
					nextUpperCase = false;
				} else {
					sb.append(Character.toLowerCase(c));
				}
				break;
			}
		}

		if (firstCharacterUppercase) {
			sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		}

		return sb.toString();
	}
	
	/**
	 * This method ensures that the specified input string is a valid
	 * Java property name.  The rules are as follows:
	 * 
	 * 1. If the first character is lower case, then OK
	 * 2. If the first two characters are upper case, then OK
	 * 3. If the first character is upper case, and the second character is lower case,
	 *    then the first character should be made lower case
	 * 
     * eMail     > eMail
     * firstName > firstName
     * URL       > URL
     * XAxis     > XAxis
     * a         > a
     * B         > b
     * Yaxis     > yaxis
     *             
	 * @param inputString
	 * @return the valid poperty name
	 */
	public static String getValidPropertyName(String inputString) {
        String answer;
        
        if (inputString == null) {
            answer = null;
        } else if (inputString.length() < 2) {
	        answer = inputString.toLowerCase(Locale.US);
	    } else {
	        if (Character.isUpperCase(inputString.charAt(0))
	            && !Character.isUpperCase(inputString.charAt(1))) {
                    answer = inputString.substring(0, 1).toLowerCase(Locale.US) +
                        inputString.substring(1);
	        } else {
                answer = inputString;
            }
        }
        
        return answer;
	}
	
	/**此方法用于自动将MAP中的值封装到 javaBean中  
 	*
	* @param map 存储着名称和值集合 
	* @param obj 要封装的对象 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	* @return封装好的对象 
	*/ 
	public static Object map2Bean(Map map,Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{ 
	   //遍历所有名称 
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			//取得名称 
			String name = it.next().toString();
			
			//取得值 
			Object v=map.get(name);
			if(v == null){
				continue;
			}
			String value = map.get(name).toString();

			name=JavaBeansUtil.getCamelCaseString(name, false);
			//取得值的类形 
			Class type = PropertyUtils.getPropertyType(obj, name);

			if (type != null) {
				//设置参数 
				PropertyUtils.setProperty(obj, name, ConvertUtils.convert(
						value, type));

			}

		}
		return obj; 
	   
	} 

	
}
